﻿using System;
using System.Data;

public partial class Guarantors : System.Web.UI.Page
{
    ServerObject mServerObject = null;

    private void ShowPanel(string panelName)
    {
        #region Hide panels
        emptyPanel.Visible = false;

        allListPanel.Visible = false;
        editEntryPanel.Visible = false;
        addNewEntryPanel.Visible = false;

        #endregion Hide panels

        try
        {
            #region Get Panel Name

            switch (panelName)
            {

                case "allListPanel":
                    allListPanel.Visible = true;

                    DataTable sourceDataTable = mServerObject.GetGuarantorsList();
                    listGridView.DataSource = sourceDataTable;
                    listGridView.DataBind();
                    break;               

                case "emptyPanel":
                    emptyPanel.Visible = true;
                    break;

                case "addNewEntryPanel":
                    addNewEntryPanel.Visible = true;
                    {
                        DataTable surseVenitList = Utils.ModuleMain().GetClassifierByTypeID((int)Constants.Constants.ClassifierTypes.SursaDeVenit);
                        Utils.FillSelector(addNewSurseVenitDropDownList, surseVenitList, "Name", "Code");

                        DataTable jobListList = Utils.ModuleMain().GetClassifierByTypeID((int)Constants.Constants.ClassifierTypes.LocDeMunca);
                        Utils.FillSelector(addNewJobDropDownList, jobListList, "Name", "Code");

                        DataTable countryList = Utils.ModuleMain().GetClassifierByTypeID((int)Constants.Constants.ClassifierTypes.CountryList);
                        Utils.FillSelector(addNewTaraDropDownList, countryList, "Name", "Code");
                    }

                    break;

                case "editEntryPanel":
                    editEntryPanel.Visible = true;
                    {
                        DataTable surseVenitList = Utils.ModuleMain().GetClassifierByTypeID((int)Constants.Constants.ClassifierTypes.SursaDeVenit);
                        Utils.FillSelector(editSursaVenitDropDownList, surseVenitList, "Name", "Code");

                        DataTable jobListList = Utils.ModuleMain().GetClassifierByTypeID((int)Constants.Constants.ClassifierTypes.LocDeMunca);
                        Utils.FillSelector(editJobDropDownList, jobListList, "Name", "Code");

                        DataTable countryList = Utils.ModuleMain().GetClassifierByTypeID((int)Constants.Constants.ClassifierTypes.CountryList);
                        Utils.FillSelector(editTaraDropDownList, countryList, "Name", "Code");
                    }

                    break;

                default:
                    emptyPanel.Visible = true;
                    break;
            }

            #endregion Get Panel Name

        }
        catch (Exception ex)
        {
            Utils.InfoText(this, "Atentie! Eroare in sistem!", ex.Message);
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (mServerObject == null) mServerObject = new ServerObject();

        string areaGUID = string.Empty;
        if (Request["area"] != null)
        {
            areaGUID = Request["area"].ToString();
        }

        #region Display Panels depending of area

        if (Utils.AutentificatedUser)
        {
            if (!IsPostBack)
            {
                string areaName = areaGUID;

                switch (areaName)
                {
                    case "allList":
                        ShowPanel(allListPanel.ID);
                        break;

                    default:
                        ShowPanel(string.Empty);
                        break;
                }
            }
        }
        else
        {
            ShowPanel(string.Empty);
        }
        #endregion Display Panels depending of area

    }

    protected void newButton_Click(object sender, EventArgs e)
    {
        ClearControlAddNewEntryPanel();
        ShowPanel(addNewEntryPanel.ID);
    }

    protected void addNewCancelButton_Click(object sender, EventArgs e)
    {
        ClearControlAddNewEntryPanel();
        ShowPanel(allListPanel.ID);
    }   

    protected void addNewSaveButton_Click(object sender, EventArgs e) //todo: impliment id from selector
    {
        try
        {
            string name = addNewTextBox.Text.Trim();
            int taraID=0;
            string tara = addNewTaraDropDownList.SelectedValue.ToString();
            int.TryParse(tara, out taraID);

            int raionID=0;
            string raioin = addNewRaionDropDownList.SelectedValue.ToString();
            int.TryParse(raioin, out raionID);

            string localitate = addNewLocalitateTextBox.Text.Trim();
            string address = addNewAddressTextBox.Text.Trim();
            string phoneFix	= addNewPhoneFixTextBox.Text.Trim();
            string phoneGSM	= addNewPhoneGsmTextBox.Text.Trim();

            int jobID=0;
            string job = addNewJobDropDownList.SelectedValue.ToString();
            int.TryParse(job, out jobID);

            int surseVenitID=0;
            string surseVenit = addNewSurseVenitDropDownList.SelectedValue.ToString();
            int.TryParse(surseVenit, out surseVenitID);

            int venitLunar = 0;
            int.TryParse(addNewVenitLunarTextBox.Text.Trim(), out venitLunar);

            if (mServerObject.AddGuarantors(name, taraID, raionID, localitate, address, phoneFix, phoneGSM, jobID, surseVenitID, venitLunar))
            {
                ClearControlAddNewEntryPanel();
                ShowPanel(allListPanel.ID);
            }
            else
            {
                Utils.InfoText(this, "Atentie", " { " + name + " } - nu a fost salvat. Verificati conectiunea cu internet ");
            }
        }
        catch (Exception ex)
        { Utils.InfoText(this, "Eroare in System!", ex.Message.ToString()); }
    }

    protected void listGridView_RowEditing(object sender, System.Web.UI.WebControls.GridViewEditEventArgs e)
    {
        try
        {
            int index = e.NewEditIndex;
            ShowPanel(editEntryPanel.ID);
            guarantorsIDLabel.Text = listGridView.Rows[index].Cells[0].Text;
            editNameTextBox.Text = listGridView.Rows[index].Cells[1].Text;

            if (!listGridView.Rows[index].Cells[2].Text.Equals(string.Empty))
                editTaraDropDownList.SelectedValue = listGridView.Rows[index].Cells[2].Text;

            FilleditRaionDropDownList();

            if (!listGridView.Rows[index].Cells[4].Text.Equals(string.Empty))
                editRaionDropDownList.SelectedValue = listGridView.Rows[index].Cells[4].Text;

            editLocalitateTextBox.Text = listGridView.Rows[index].Cells[6].Text;
            editAddressTextBox.Text = listGridView.Rows[index].Cells[7].Text;
            editPhoneFixTextBox.Text = listGridView.Rows[index].Cells[8].Text;
            editPhoneGSMTextBox.Text = listGridView.Rows[index].Cells[9].Text;

            if (!listGridView.Rows[index].Cells[10].Text.Equals(string.Empty))
            editJobDropDownList.SelectedValue = listGridView.Rows[index].Cells[10].Text;

            if (!listGridView.Rows[index].Cells[12].Text.Equals(string.Empty))
            editSursaVenitDropDownList.SelectedValue = listGridView.Rows[index].Cells[12].Text;

            editVenitLunarTextBox.Text = listGridView.Rows[index].Cells[14].Text;
        }
        catch (Exception ex)
        {
            Utils.InfoText(this, "Atentie! Eroare in sistem!", ex.Message);
        }        
    }

    protected void editSaveButton_Click(object sender, EventArgs e)
    {
        try
        {
            string strTypeID = guarantorsIDLabel.Text;
            int guarantorsID = 0;
            int.TryParse(strTypeID, out guarantorsID);

            string name = editNameTextBox.Text.Trim();
            
            int taraID = 0;
            string tara = editTaraDropDownList.SelectedValue.ToString();
            int.TryParse(tara, out taraID);

            int raionID = 0;
            string raion = editRaionDropDownList.SelectedValue.ToString();
            int.TryParse(raion, out raionID);

            string localitate = editLocalitateTextBox.Text.Trim();
            string address = editAddressTextBox.Text.Trim();
            string phoneFix = editPhoneFixTextBox.Text.Trim();
            string phoneGSM = editPhoneGSMTextBox.Text.Trim();

            int jobID = 0;
            string job = editJobDropDownList.SelectedValue.ToString();
            int.TryParse(job, out jobID);

            int surseVenitID = 0;
            string surseVenit = editSursaVenitDropDownList.SelectedValue.ToString();
            int.TryParse(surseVenit, out surseVenitID);

            int venitLunar = 0;
            int.TryParse(editVenitLunarTextBox.Text.Trim(), out venitLunar);

            if (mServerObject.UpdateGuarantors(guarantorsID, name, taraID, raionID, localitate, address, phoneFix, phoneGSM, jobID, surseVenitID, venitLunar))
            {
                ClearControlEditPanel();
                ShowPanel(allListPanel.ID);
            }
            else
            {
                Utils.InfoText(this, "Atentie", " { " + name + " } - nu a fost salvat. Verificati conectiunea internetului ");
            }
        }
        catch (Exception ex)
        { Utils.InfoText(this, "Eroare in System!", ex.Message.ToString()); }
    }

    protected void listGridView_RowDeleting(object sender, System.Web.UI.WebControls.GridViewDeleteEventArgs e)
    {
        int index = e.RowIndex;

        try
        {
            string strGuarantosID = listGridView.Rows[index].Cells[0].Text;
            int guarantosID = 0;
            int.TryParse(strGuarantosID, out guarantosID);

            if (mServerObject.DeleteGuarantors(guarantosID))
            {
                ShowPanel(allListPanel.ID);
            }
            else
            {
                Utils.InfoText(this, "Atentie", " Stergerea garantului nu a fost efectuata. Verificati conectiunea internetului ");
            }
        }
        catch (Exception ex)
        { Utils.InfoText(this, "Eroare in System!", ex.Message.ToString()); }
    }

    protected void editCancelButton_Click(object sender, EventArgs e)
    {
        ClearControlEditPanel();
        ShowPanel(allListPanel.ID);
    }

    protected void listGridView_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
    {
        listGridView.PageIndex = e.NewPageIndex;
        ShowPanel(allListPanel.ID);
    }

    #region ClearControls

    private void ClearControlAddNewEntryPanel()
    {
        addNewTextBox.Text = string.Empty;
        addNewTaraDropDownList.SelectedIndex = -1;
        addNewRaionDropDownList.SelectedIndex = -1;
        addNewLocalitateTextBox.Text = string.Empty;
        addNewAddressTextBox.Text = string.Empty;
        addNewPhoneFixTextBox.Text = string.Empty;
        addNewPhoneGsmTextBox.Text = string.Empty;
        addNewJobDropDownList.SelectedIndex = -1;
        addNewSurseVenitDropDownList.SelectedIndex = -1;
        addNewVenitLunarTextBox.Text = string.Empty;
    }

    private void ClearControlEditPanel()
    {
        editNameTextBox.Text = string.Empty;
        editTaraDropDownList.SelectedIndex = -1;
        editRaionDropDownList.SelectedIndex = -1;
        editLocalitateTextBox.Text = string.Empty;
        editAddressTextBox.Text = string.Empty;
        editPhoneFixTextBox.Text = string.Empty;
        editPhoneGSMTextBox.Text = string.Empty;
        editJobDropDownList.SelectedIndex = -1;
        editSursaVenitDropDownList.SelectedIndex = -1;
        editVenitLunarTextBox.Text = string.Empty;
    }

    #endregion CleaControls

    #region SelectedIndexChanged

    protected void FilleditRaionDropDownList()
    {
        if (editTaraDropDownList.SelectedValue != null && !editTaraDropDownList.SelectedValue.ToString().Equals(string.Empty))
        {
            int countruCOde = 0;
            int.TryParse(editTaraDropDownList.SelectedValue.ToString(), out countruCOde);

            DataTable b_provinceList = Utils.ModuleMain().GetProvinceListByCountry(countruCOde);
            Utils.FillSelector(editRaionDropDownList, b_provinceList, "Name", "Code");
        }
    }

    protected void addNewTaraDropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (addNewTaraDropDownList.SelectedValue != null && !addNewTaraDropDownList.SelectedValue.ToString().Equals(string.Empty))
        {
            int countruCOde = 0;
            int.TryParse(addNewTaraDropDownList.SelectedValue.ToString(), out countruCOde);

            DataTable b_provinceList = Utils.ModuleMain().GetProvinceListByCountry(countruCOde);
            Utils.FillSelector(addNewRaionDropDownList, b_provinceList, "Name", "Code");
        }
    }
    
    protected void editTaraDropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        FilleditRaionDropDownList();
    }

    #endregion SelectedIndexChanged
}